package com.jy.appmarket.view;

import com.jy.appmarket.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 类说明：
 * 
 * @author Terry.Lu
 * @date 2013-5-29
 * @version 1.0
 */
public class LoadingView extends RelativeLayout implements View.OnClickListener {
	private ImageView mIcon;
	private ProgressWheel mProgressWheel;
	private TextView mTextView;
	private OnClickListener mListener;

	public interface OnClickListener {
		void onLoadingViewClick(View view);
	}

	public LoadingView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public LoadingView(Context context) {
		super(context);
		init();
	}

	private void init() {
		LayoutInflater li = LayoutInflater.from(getContext());
		View v = li.inflate(R.layout.loading_view_info, this, false);
		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		lp.addRule(RelativeLayout.CENTER_IN_PARENT);
		addView(v, lp);
		mIcon = findViewById(R.id.loading_info_icon);
		mTextView = findViewById(R.id.loading_info_text);
		mProgressWheel = findViewById(R.id.loading_info_progress);
		setOnClickListener(this);
	}

	public void showWaitView(int resId) {
		mTextView.setText(resId);
		showWaitView();
	}

	public void showWaitView(String tip) {
		mTextView.setText(tip);
		showWaitView();
	}

	public void setTextColor(int color) {
		mTextView.setTextColor(color);
	}

	private void showWaitView() {
		setClickable(false);
		mIcon.setImageResource(R.drawable.loading_view_loading);
		mProgressWheel.setVisibility(View.VISIBLE);
	}

	public void showErrorView(String tip) {
		mTextView.setText(tip);
		showErrorView();
	}

	public void showErrorView(int tip) {
		mTextView.setText(tip);
		showErrorView();
	}

	private void showErrorView() {
		setClickable(true);
		mIcon.setImageResource(R.drawable.loading_view_error);
		mProgressWheel.setVisibility(View.GONE);
	}

	private void showEmptyView() {
		setClickable(false);
		mIcon.setImageResource(R.drawable.loading_view_empty);
		mProgressWheel.setVisibility(View.GONE);
	}

	public void showEmptyView(String tip) {
		mTextView.setText(tip);
		showEmptyView();
	}

	public void showEmptyView(int tip) {
		mTextView.setText(tip);
		showEmptyView();
	}

	@Override
	public void onClick(View v) {
		if (mListener != null) {
			mListener.onLoadingViewClick(v);
		}
	}

	public void setOnClickListener(LoadingView.OnClickListener l) {
		mListener = l;
	}

	public void setImage(int resId) {
		ImageView logo = findViewById(R.id.loading_info_icon);
		logo.setImageResource(resId);
	}
	
	public void showEmptyView(String tip, int icon_id){
		mTextView.setText(tip);
		setClickable(false);
		mIcon.setImageResource(icon_id);
		mProgressWheel.setVisibility(View.GONE);
	}
}
